package cn.wingware.util;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 促销相关工具类
 * 
 * @author MaChao
 * 
 */
public class PromotionUtil {

	/**
	 * 获取正在进行的促销计划列表
	 * 
	 * @param promotionList
	 *            正常状态的促销计划列表
	 * @return
	 * @throws Exception
	 */
	public List<Map<String, Object>> getPromotingList(List<Map<String, Object>> promotionList) throws Exception {
		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
		for (Map<String, Object> promotion : promotionList) {
			if (checkPromoting(promotion, new Date())) {
				result.add(promotion);
			}
		}
		promotionList = null;
		return result;
	}

	/**
	 * 检查指定时间时促销计划是否正在生效（考虑日期及时间）
	 * 
	 * @param Map
	 *            <String,Object> promotion
	 * @param Date
	 *            d
	 * @return boolean true=促销计划正在生效;false=促销计划未生效
	 * @throws Exception
	 */
	public static boolean checkPromoting(Map<String, Object> promotion, Date d) throws Exception {
		boolean result = false;
		String perriod = promotion.get("period") != null ? promotion.get("period").toString() : null;
		if ((5 == PromotionUtil.checkPromotionStatus(promotion, d)) && PromotionUtil.checkPromotionPeriod(perriod, d)) {
			result = true;
		}
		return result;
	}

	/**
	 * 检查指定日期促销计划的状态（不考虑时间段）
	 * 
	 * @param Map
	 *            <String,Object> promotion
	 * @param Date
	 *            d
	 * @return int 0=异常;2=已删除;3=已终止;4=未开始;5=进行中;6=已结束;
	 * @throws ParseException
	 */
	public static int checkPromotionStatus(Map<String, Object> promotion, Date d) throws ParseException {

		int result = 0;
		// 异常
		if (null == promotion || null == d) {
			result = 0;
			return result;
		}
		int status = promotion.get("status") != null ? Integer.parseInt(promotion.get("status").toString()) : 0;
		Date beginDate = (Date) promotion.get("begin_date");
		Date endDate = (Date) promotion.get("end_date");
		String period = promotion.get("period") != null ? promotion.get("period").toString() : null;
		// 异常
		if (0 == status) {
			result = 0;
			return result;
		}

		// 已删除
		if (2 == status) {
			result = 2;
			return result;
		}

		// 已终止
		if (3 == status) {
			result = 3;
			return result;
		}

		// 正常状态
		if (1 == status && (null != period && !("").equals(period.trim()))) {
			java.text.SimpleDateFormat parseTime = new java.text.SimpleDateFormat("yyyy-MM-dd");
			beginDate = parseTime.parse(parseTime.format(beginDate));
			endDate = parseTime.parse(parseTime.format(endDate));
			d = parseTime.parse(parseTime.format(d));
		}
		if (1 == status) {
			// 未开始
			if (d.getTime() < beginDate.getTime()) {
				result = 4;
				return result;
			}
			// 进行中
			if (beginDate.getTime() <= d.getTime() && d.getTime() <= endDate.getTime()) {
				result = 5;
				return result;
			}
			// 已结束
			if (d.getTime() > beginDate.getTime()) {
				result = 6;
				return result;
			}
		}
		return result;
	}

	/**
	 * 检查指定时间是否在促销计划的有效时段内（仅考虑时间）
	 * 
	 * @param String
	 *            period 促销时段，String类型，冒号减号分割，如：08:00-10:00
	 * @param Date
	 *            d 被检查时间
	 * @return boolean true=在促销时段内;false=非促销时段
	 * @throws Exception
	 */
	public static boolean checkPromotionPeriod(String period, Date d) throws Exception {
		/*
		 * 思路：将起始时间、结束时间、被比较时间全部转为1900-01-01当天的Date类型对象。
		 * 通过Date类型的getTimes()方法进行比较。 如果period为空，则不考虑是否在有效时段内，返回true
		 */
		if (null == period || "".equals(period))
			return true;

		boolean result = false;
		String[] p = period.split("-");
		if (p.length != 2) {
			throw new Exception("PromotionUtil.checkPromotionPeriod:invalid format for period.");
		}
		// 起始时间
		String beginTime = p[0];
		// 结束时间
		String endTime = p[1];
		// 起始日期
		Date beginDate = DateUtil.praseString2LongDate("1900-01-01 " + beginTime + ":00");
		// 结束日期
		Date endDate = DateUtil.praseString2LongDate("1900-01-01 " + endTime + ":59");

		Calendar cal = Calendar.getInstance();
		cal.setTime(d);
		// 被比较时间
		String targetTime = cal.get(Calendar.HOUR_OF_DAY) + ":" + cal.get(Calendar.MINUTE) + ":" + cal.get(Calendar.SECOND);
		// 被比较日期
		Date targetDate = DateUtil.praseString2LongDate("1900-01-01 " + targetTime);

		// 进行比较
		if (beginDate.getTime() <= targetDate.getTime() && targetDate.getTime() <= endDate.getTime()) {
			result = true;
		}

		return result;
	}

}